package common.util;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 公共工具
 */
public class CommonUtil {

	/**
	 * 字符编码转换（"ISO8859-1" --> "UTF-8"）
	 *
	 * @param fileName
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String toCNCode(String fileName) throws UnsupportedEncodingException {
		fileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");
		return fileName;
	}

	/**
	 * 字符编码转换（"UTF-8" --> "ISO8859-1"）
	 *
	 * @param fileName
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String toENCode(String fileName) throws UnsupportedEncodingException {
		fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
		return fileName;
	}

	public static String toUtf8String(String fileName) throws UnsupportedEncodingException {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < fileName.length(); i++) {
			char c = fileName.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes("utf-8");
				} catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0) k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 将POJO对象转换为JSON字符串
	 *
	 * @param bean
	 * @return
	 */
	public static String toJSON(Object bean) {
		ObjectMapper mapper = new ObjectMapper();
		StringWriter sw = new StringWriter();
		JsonGenerator gen = null;
		try {
			gen = new JsonFactory().createJsonGenerator(sw);
			mapper.writeValue(gen, bean);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				gen.close();
			} catch (IOException e) {
				e.printStackTrace();
				return "{}";
			}
		}
		String json = sw.toString();
		return json;
	}


	/**
	 * 求阶乘
	 *
	 * @param number
	 * @return
	 */
	public static long factorial(long number) {
		if (number <= 1) {
			return 1;
		} else {
			return number * factorial(number - 1);
		}
	}

	/**
	 * 排列组合的排列
	 *
	 * @param down
	 * @param up
	 * @return
	 */
	public static long arrangement(int down, int up) {
		return factorial(down) / factorial(down - up);
	}

	/**
	 * 排列组合的组合
	 *
	 * @param down
	 * @param up
	 * @return
	 */
	public static long combination(int down, int up) {
		return factorial(down) / (factorial(up) * factorial(down - up));

	}

	/**
	 * 取组合数
	 *
	 * @param a
	 * @param m
	 * @return
	 */
	public static List combine(int[] a, int m) {
		int n = a.length;
		if (m > n) {
			// throw new OurException("错误！数组a中只有"+n+"个元素。"+m+"大于"+2+"!!!");
			try {
				throw new Exception("错误！数组a中只有" + n + "个元素。" + m + "大于" + 2 + "!!!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//返回list初始化
		List result = new ArrayList();

		int[] bs = new int[n];
		for (int i = 0; i < n; i++) {
			bs[i] = 0;
		}
		//初始化
		for (int i = 0; i < m; i++) {
			bs[i] = 1;
		}
		boolean flag = true;
		boolean tempFlag = false;
		int pos = 0;
		int sum = 0;
		//首先找到第一个10组合，然后变成01，同时将左边所有的1移动到数组的最左边
		while (flag) {
			//do{
			sum = 0;
			pos = 0;
			tempFlag = true;
			result.add(print(bs, a, m));

			for (int i = 0; i < n - 1; i++) {
				if (bs[i] == 1 && bs[i + 1] == 0) {
					bs[i] = 0;
					bs[i + 1] = 1;
					pos = i;
					break;
				}
			}
			//将左边的1全部移动到数组的最左边

			for (int i = 0; i < pos; i++) {
				if (bs[i] == 1) {
					sum++;
				}
			}
			for (int i = 0; i < pos; i++) {
				if (i < sum) {
					bs[i] = 1;
				} else {
					bs[i] = 0;
				}
			}

			//检查是否所有的1都移动到了最右边
			for (int i = n - m; i < n; i++) {
				if (bs[i] == 0) {
					tempFlag = false;
					break;
				}
			}
			if (!tempFlag) {
				flag = true;
			} else {
				flag = false;
			}

			// } while (flag);
		}
		result.add(print(bs, a, m));

		return result;
	}

	/**
	 * 搭配上面取组合使用
	 *
	 * @param bs
	 * @param a
	 * @param m
	 * @return
	 */
	private static int[] print(int[] bs, int[] a, int m) {
		int[] result = new int[m];
		int pos = 0;
		for (int i = 0; i < bs.length; i++) {
			if (bs[i] == 1) {
				result[pos] = a[i];
				pos++;
			}
		}
		return result;
	}


	/**
	 * 获取时间戳的方法 synchronized
	 *
	 * @return
	 */
	public static synchronized String getCurrentTimeStamp() {
		return DateUtil.nowTime();
	}

	/**
	 * test
	 *
	 * @param args
	 */
	public static void main(String args[]) {

		//		System.out.println(pagingCalculate(10, 2));

		//		Map<String, Object> map = new HashMap<String, Object>();
		//		map.put("str", "abcd");
		//		List<String> list = new ArrayList<String>();
		//		list.add("1");
		//		list.add("2");
		//		map.put("list", list);
		//		System.out.println(toJSON(map));


	}

}
